package com.dream.myqiyi.ui;

import com.dream.myqiyi.R;
import com.dream.myqiyi.service.ApplicationContext;
import com.dream.myqiyi.service.NotebookService;
import com.dream.myqiyi.service.TagService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TagViewActivity extends Activity {

	private EditText mTagNameEdit;
	private String tagName = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tag_view_activity);
		
		mTagNameEdit = (EditText)findViewById(R.id.tag_view_name_edit);
		
		Button okBtn = (Button)findViewById(R.id.tag_view_ok_btn);
		Button retBtn = (Button)findViewById(R.id.tag_view_reback_btn);
		
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if("".equals(mTagNameEdit.getText().toString())) {
		        	new AlertDialog.Builder(TagViewActivity.this)
					.setIcon(getResources().getDrawable(R.drawable.login_error_icon))
					.setTitle("创建标签错误")
					.setMessage("标签名字为空！")
					.create().show();
				} else {
					tagName = mTagNameEdit.getText().toString();
					new SaveTagTask().execute("");
					
					Intent intent = new Intent (TagViewActivity.this, TagListActivity.class);			
					startActivity(intent);			
					TagViewActivity.this.finish();
				}
				
			}
			
		});
		
		retBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
//				MainActivity.instance.back();
//				TagViewActivity.this.finish();
				Intent intent = new Intent (TagViewActivity.this, TagListActivity.class);			
				startActivity(intent);			
				TagViewActivity.this.finish();

				//MainActivity.instance.replaceView(2);		
			}
			
		});
	}

	private class SaveTagTask extends AsyncTask<String, String, String> {
		public String doInBackground(String... params) {	
			if (!tagName.equals("")) {
				TagService tagSrv = (TagService)ApplicationContext.getSingleton().getBean("TagService");
				if (tagSrv != null) {
					tagSrv.addTag(tagName);
				}
			}

			return "";
		}

		@Override
		public void onPostExecute(String Re) {
		}
	}

}
